
function Page(url) {
	this.pageSize = 10;
	this.first = 1;
	this.max = null;
	this.url = url;
	this.para = "";
	this.pagePara = "";
	this.totalRows = 0;
	this.currentPage = 0;
	this.totalPage = 0;
	this.prefix = "";
	this.pageTD = null;
	this.pageTR = null;
	this.titleRows = null;
	this.oddClass = null;
	this.initial = false;
	this.evenClass = "even";
	this.hoverClass = "hover";
	this.stripe = true;
	this.hover = false;
	this.callfun = null;
}
Page.prototype.setPageSize = function (pageSize) {
	this.pageSize = pageSize;
};
Page.prototype.setStripe = function (stripe) {
	this.stripe = stripe;
};
Page.prototype.setEvenClass = function (evenClass) {
	this.evenClass = evenClass;
};
Page.prototype.setHover = function (hover) {
	this.hover = hover;
};
Page.prototype.setHoverClass = function (hoverClass) {
	this.hoverClass = hoverClass;
};
Page.prototype.setFirst = function () {
	this.first = 1;
	this.max = this.pageSize;
};
Page.prototype.init = function () {
	this.titleRows = $("#" + this.prefix + "_pageList>tbody>tr").length - 1;
	this.oddClass = $("#" + this.prefix + "_pageList>tbody>tr:eq(" + this.titleRows + ")").attr("class");
	this.pageTR = $("#" + this.prefix + "_pageList>tbody>tr:eq(" + this.titleRows + ")");
	this.pageTD = $("#" + this.prefix + "_pageList>tbody>tr:eq(" + this.titleRows + ")>td");
	this.max = this.pageSize;
	var p = this;
	$("#" + p.prefix + "_home").click(function () {
		p.setFirst();
		p.query();
	});
	$("#" + p.prefix + "_previous").click(function () {
		p.max = p.first - 1;
		p.first = p.first - p.pageSize;
		p.query();
	});
	$("#" + p.prefix + "_next").click(function () {
		p.first = p.max + 1;
		p.max = p.max + p.pageSize;
		p.query();
	});
	$("#" + p.prefix + "_last").click(function () {
		p.first = (p.totalPage - 1) * p.pageSize + 1;
		p.max = p.first + p.pageSize - 1;
		p.query();
	});
	$("#" + p.prefix + "_go").click(function () {
		var goValue = $("#" + p.prefix + "_goValue").val();
		if (goValue >= 1 && goValue <= p.totalPage && goValue.indexOf(".") < 0) {
			p.first = (goValue - 1) * p.pageSize + 1;
			p.max = goValue * p.pageSize;
			p.query();
			$("#" + p.prefix + "_goValue").val("");
		} else {
			alert("\u9875\u6570\u5fc5\u987b\u5927\u4e8e\u7b49\u4e8e1\u5c0f\u4e8e\u7b49\u4e8e" + p.totalPage);
			$("#" + p.prefix + "_goValue")[0].focus();
		}
	});
	this.initial = true;
};
Page.prototype.query = function (para) {
	showMask(this.prefix);
	if (!this.initial) {
		this.init();
	}
	if (para != null && para != "") {
		this.para = "," + para;
	}
	if (para == "") {
		this.para = para;
	}
	if (this.pagePara != "" && this.pagePara.indexOf(",") != 0) {
		this.pagePara = "," + this.pagePara;
	}
	var dataStr = "{\"page\":{\"first\":" + this.first + ",\"max\":" + this.max + this.pagePara + "}" + this.para + "}";
	$.ajax({type:"POST", url:this.url, data:dataStr, page:this, success:this.callback, error:function (XMLHttpRequest, textStatus, errorThrown) {
		document.write("json " + textStatus + XMLHttpRequest.responseText);
	}});
};
Page.prototype.handleDisplay = function () {
	if (this.pageSize >= this.totalRows) {
		$("#" + this.prefix + "_home").attr("disabled", "disabled");
		$("#" + this.prefix + "_previous").attr("disabled", "disabled");
		$("#" + this.prefix + "_next").attr("disabled", "disabled");
		$("#" + this.prefix + "_last").attr("disabled", "disabled");
	} else {
		if (this.currentPage == 1) {
			$("#" + this.prefix + "_home").attr("disabled", "disabled");
			$("#" + this.prefix + "_previous").attr("disabled", "disabled");
			$("#" + this.prefix + "_next").removeAttr("disabled");
			$("#" + this.prefix + "_last").removeAttr("disabled");
		} else {
			if (this.currentPage == this.totalPage) {
				$("#" + this.prefix + "_home").removeAttr("disabled");
				$("#" + this.prefix + "_previous").removeAttr("disabled");
				$("#" + this.prefix + "_next").attr("disabled", "disabled");
				$("#" + this.prefix + "_last").attr("disabled", "disabled");
			} else {
				$("#" + this.prefix + "_home").removeAttr("disabled");
				$("#" + this.prefix + "_previous").removeAttr("disabled");
				$("#" + this.prefix + "_next").removeAttr("disabled");
				$("#" + this.prefix + "_last").removeAttr("disabled");
			}
		}
	}
};
Page.prototype.callback = function (json) {
	var p = this.page;
	p.totalRows = json.totalRows;
	$("#" + p.prefix + "_totalRows").text(p.totalRows);
	p.totalPage = parseInt(p.totalRows / p.pageSize);
	if (p.totalRows % p.pageSize > 0) {
		p.totalPage += 1;
	}
	$("#" + p.prefix + "_totalPage").text(p.totalPage);
	p.currentPage = (p.first + p.pageSize - 1) / p.pageSize;
	$("#" + p.prefix + "_currentPage").text(p.currentPage);
	p.handleDisplay();
	$("#" + p.prefix + "_pageFoot").show();
	var trStr = "#" + p.prefix + "_pageList>tbody>tr";
	if (p.titleRows != 0) {
		trStr = "#" + p.prefix + "_pageList>tbody>tr:gt(" + (p.titleRows - 1) + ")";
	}
	var tdLength = $(trStr + ":eq(0)>td").length;
	$(trStr).remove();
	var rownum = 0;
	$.each(json.list, function (i, obj) {
		var no = (++rownum) + (p.currentPage - 1) * p.pageSize;
		if (p.stripe) {
			if (i % 2 != 0) {
				p.pageTR.attr("class", p.evenClass);
			} else {
				p.pageTR.attr("class", p.oddClass);
			}
		}
		var newTr = p.pageTR[0].cloneNode(false);
		$(newTr).show();
		$.each(p.pageTD, function (j, td) {
			var newTd = td.cloneNode(false);
			$(newTd).html(replaceTd(obj, td, no));
			newTr.appendChild(newTd);
		});
		$("#" + p.prefix + "_pageList").append($(newTr));
		if (p.hover) {
			$(trStr).hover(function () {
				$(this).addClass(p.hoverClass);
			}, function () {
				$(this).removeClass(p.hoverClass);
			});
		}
	});
	if (json.list.length == 0) {
		if ($("#" + p.prefix + "_nodata>td:eq(0)").attr("colspan") == "1") {
			$("#" + p.prefix + "_nodata>td:eq(0)").attr("colspan", tdLength);
		}
		$("#" + p.prefix + "_pageFoot").hide();
		$("#" + p.prefix + "_nodata").clone().appendTo($("#" + p.prefix + "_pageList"));
	}
	if (p.callfun) {
		p.callfun.call(this, json);
	}
	hideMask(p.prefix);
};
Page.prototype.orderBy = function (imgObj, cName) {
	var imgSrc = imgObj.src;
	$("#" + this.prefix + "_pageList>tbody>tr:eq(0) img").attr("src", basePath + "b2b/img/order-no.png");
	var lastStr = cName.indexOf(",") > 0 ? cName.substring(cName.indexOf(",")) : "";
	if (imgSrc.indexOf("order-no.png") > 0 || imgSrc.indexOf("order-down.png") > 0) {
		imgObj.src = basePath + "b2b/img/order-up.png";
		this.pagePara = "\"orderBy\":\"" + cName.split(",")[0] + " desc" + lastStr + "\"";
	} else if (imgSrc.indexOf("order-up.png") > 0) {
		imgObj.src = basePath + "b2b/img/order-down.png";
		this.pagePara = "\"orderBy\":\"" + cName.split(",")[0] + " asc" + lastStr + "\"";
	}
};

